package com.skcc.ts.nsi.service;

import java.util.Calendar;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.skcc.ts.nsi.MEIntro;
import com.skcc.ts.nsi.MEMain;
import com.skcc.ts.nsi.expense.MEExpenseDetail;
import com.skcc.ts.nsi.expense.MEExpenseListMjs;
import com.skcc.ts.nsi.expense.MEExpenseListMjsSms;
import com.skcc.ts.nsi.expense.MEExpenseListResult;
import com.skcc.ts.nsi.expense.MEExpenseListSearch;
import com.skcc.ts.nsi.expense.MEExpenseListUpdate;
import com.skcc.ts.nsi.expense.MEExpenseMgmt;
import com.skcc.ts.nsi.expense.MEExpenseRegister;
import com.skcc.ts.nsi.project.MEPjtMain;
import com.skcc.ts.nsi.user.MEUsrInPjtSearch;
import com.skcc.ts.nsi.user.MEUsrMain;
import com.skcc.ts.nsi.user.MEUsrPrjRegist;
import com.skcc.ts.nsi.user.MEUsrRegiSearch;
import com.skcc.ts.nsi.vo.MEAcntVo;
import com.skcc.ts.nsi.vo.MECardVo;
import com.skcc.ts.nsi.vo.MEExpVo;
import com.skcc.ts.nsi.vo.MEPjtVo;
import com.skcc.ts.nsi.vo.MEUsrVO;

public class SocketClientService extends Service {

	private String loginId;
	private String loginPw;
	private String myId;
	private String addr;
	private String port;
	private String insSmsMjsList = "false";
	private String[] cmd = null;
	private String[] data = null;
	private MEUsrVO usrVo = null;
	private MEPjtVo pjtVo = null;
	private MEExpVo expVo = null;
	private MECardVo cbVo = null;
	private MEAcntVo[] acntVo = null;
	private MEAcntVo acntVo_exp = null;

	public void onCreate() {
		Log.d("debug", "now service");
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

		int function = intent.getIntExtra("id", 0);

		if (function == 10) {// SMS수신시
			loginId = intent.getStringExtra("loginId");
			loginPw = intent.getStringExtra("loginPw");
			String date = intent.getStringExtra("date");
			String cost = intent.getStringExtra("cost");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");

			String distributor = intent.getStringExtra("distributor"); // SMS수신내역 중 사용처 정보

			insSmsMjsList = "false";

			cmd = new String[1];
			data = new String[1];

			// date변수 포맷 : 201210091200, 상호명, 위치 null값 입력..
			cmd[0] = "insert_card_bill|" + date + "|" + loginId + "|" + cost + "|" + distributor + "|" + "내용없음" + "|N|" + "|S";
			Log.d("insert_card_bill_cmd", cmd[0]);

			data = SocketHandler.communicate(cmd, addr, port);

			if ("SUCC".equals(data[0].substring(0, 4))) {
				insSmsMjsList = "true";
				Intent sendI = new Intent(SocketClientService.this, SocketClientService.class);
				sendI.putExtra("id", 433);// 기능분류(SMS미정산내역)
				sendI.putExtra("insSmsMjsList", "true");
				sendI.putExtra("myId", loginId);
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				sendI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startService(sendI);

			} else {
				Log.d("SocketClientService", "SMS수신 후, 미정산리스트등록안됨(insert_cardbill 실패)");
				mainRestart();
			}

			stopSelf();

		} else if (function == 11) {// 로그인 데이터 처리
			loginId = intent.getStringExtra("loginId");
			loginPw = intent.getStringExtra("loginPw");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");

			cmd = new String[1];
			data = new String[1];
			cmd[0] = "user_list_by_id|" + loginId;
			data = SocketHandler.communicate(cmd, addr, port);
			if ("No".equals(data[0].substring(0, 2))) {
				loginRestart("wrongId");
			} else {
				usrVo = MEDataHandler.userInfo(data[0]);
				if (!(loginPw.equals(usrVo.getMy_pw()))) {
					loginRestart("wrongPw");
				} else {
					mainStart(false);
				}
			}
			usrVo = null;
			stopSelf();

		} else if (function == 12) {// 팀원조회(프로젝트선택 전)
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];

			cmd[0] = "pjt_list_by_user|" + myId;
			data = SocketHandler.communicate(cmd, addr, port);

			pjtVo = MEDataHandler.pjtInfo(data[0]);

			if (pjtVo == null) {
				userMainRestart();
			} else {
				Intent sendI = new Intent(SocketClientService.this, MEUsrInPjtSearch.class);
				sendI.putExtra("teamProject", true);
				sendI.putExtra("myId", myId);
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				sendI.putExtra("projectCode", pjtVo.getPjt_code());
				sendI.putExtra("projectName", pjtVo.getPjt_name());
				pending(sendI);
			}
			pjtVo = null;
			stopSelf();

		} else if (function == 13) {// 팀원 리스팅
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];

			cmd[0] = "user_list_by_pjt|" + intent.getStringExtra("projectCode");
			data = SocketHandler.communicate(cmd, addr, port);
			usrVo = MEDataHandler.teamInfo(data[0]);
			String myRole = null;

			if (usrVo == null) {
				Intent sendI = new Intent(SocketClientService.this, MEUsrMain.class);
				sendI.putExtra("noone", true);
				sendI.putExtra("myId", myId);
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				pending(sendI);
			} else {
				for (int i = 0; i < usrVo.getUsr_id().length; i++) {
					if (myId.equals(usrVo.getUsr_id()[i])) {
						myRole = usrVo.getUsr_role()[i];
					}
				}

				Intent sendI = new Intent(SocketClientService.this, MEUsrRegiSearch.class);
				if ("PM".equals(myRole)) {
					sendI.putExtra("teamList", true);
				} else {
					sendI.putExtra("teamList", false);
				}
				sendI.putExtra("teamId", usrVo.getUsr_id());
				sendI.putExtra("teamName", usrVo.getUsr_name());
				sendI.putExtra("teamRole", usrVo.getUsr_role());
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				pending(sendI);
			}
			usrVo = null;
			stopSelf();
		} else if (function == 14) {// 프로젝트 등록
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[2];
			data = new String[2];
			cmd[0] = "pjt_list|";
			cmd[1] = "user_list_by_id|" + myId;
			data = SocketHandler.communicate(cmd, addr, port);
			pjtVo = MEDataHandler.pjtInfo(data[0]);
			usrVo = MEDataHandler.userInfo(data[1]);

			if (pjtVo == null) {
				userMainRestart();
			} else {
				Intent sendI = new Intent(SocketClientService.this, MEUsrPrjRegist.class);
				sendI.putExtra("start", true);
				sendI.putExtra("myId", myId);
				sendI.putExtra("myName", usrVo.getMy_name());
				sendI.putExtra("projectCode", pjtVo.getPjt_code());
				sendI.putExtra("projectName", pjtVo.getPjt_name());
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				pending(sendI);
			}
			pjtVo = null;
			usrVo = null;
			stopSelf();

		} else if (function == 15) {
			myId = intent.getStringExtra("userId");
			String pCode = intent.getStringExtra("regiPjtCode");
			String role = intent.getStringExtra("userRole");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];
			cmd[0] = "user_pjt_regist|" + pCode + "|" + myId + "|" + role;
			data = SocketHandler.communicate(cmd, addr, port);
			String result = data[0].substring(0, 4);
			Intent sendI = new Intent(SocketClientService.this, MEUsrMain.class);
			sendI.putExtra("teamRegi", true);
			sendI.putExtra("myId", myId);
			sendI.putExtra("result", result);
			sendI.putExtra("loginAddr", addr);
			sendI.putExtra("loginPort", port);
			pending(sendI);

			stopSelf();
		} else if (function == 31) {// 프로젝트관리:조회
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];

			cmd[0] = "pjt_list_by_user|" + myId;
			data = SocketHandler.communicate(cmd, addr, port);

			pjtVo = MEDataHandler.pjtInfo(data[0]);
			if (pjtVo == null) {
				mainRestart();
			} else {
				int num = listProjectRelatedAcount(addr,port);
				projectMainStart(num);
			}
			pjtVo = null;
			stopSelf(); // TODO 서비스 중지 조건분기 최종처리할것

		} else if (function == 41) {// 비용정산관리(조회)
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[2];
			data = new String[2];

			cmd[0] = "pjt_list_by_user|" + myId;
			cmd[1] = "acnt_list";

			data = SocketHandler.communicate(cmd, addr, port);

			pjtVo = MEDataHandler.pjtInfo(data[0]);
			acntVo_exp = MEDataHandler.acntBasicInfo(data[1]);

			if (pjtVo == null) {
				mainRestart();
			} else {
				expenseMainStart(false, "na", addr, port);
			}
			pjtVo = null;
			acntVo_exp = null;
			stopSelf();
		} else if (function == 42) { // 정산내역 조회
			myId = intent.getStringExtra("myId");
			String sDay = intent.getStringExtra("startDate");
			String eDay = intent.getStringExtra("endDate");
			String pCode = intent.getStringExtra("projectCode");
			String aCode = intent.getStringExtra("accountCode");
			String aName = intent.getStringExtra("accountName");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			String selection = "noacnt";
			cmd = new String[3];
			data = new String[3];
			cmd[0] = "acnt_list_by_code|" + pCode;
			cmd[1] = "pjt_list_by_user|" + myId;
			if ("-- 선택 --".equals(aCode)) {
				cmd[2] = "inqr_balance_list|" + myId + "|" + sDay + "|" + eDay + "|" + pCode + "|" + "%";
			} else if (!("-- 선택 --".equals(aCode))) {
				cmd[2] = "inqr_balance_list|" + myId + "|" + sDay + "|" + eDay + "|" + pCode + "|" + aCode;
			}
			data = SocketHandler.communicate(cmd, addr, port);
			boolean isValidAcnt = false;
			acntVo_exp = MEDataHandler.acntInfo(data[0]);
			pjtVo = MEDataHandler.pjtInfo(data[1]);
			String[] compareAcnt = new String[acntVo_exp.getAcnt_name().length];
			compareAcnt = acntVo_exp.getAcnt_name();
			for (int i = 0; i < compareAcnt.length; i++) {
				if (aName.equals(compareAcnt[i])) {
					isValidAcnt = true;
					selection = "nodata";
					break;
				}
			}

			if (!isValidAcnt && !("-- 선택 --".equals(aCode))) {
				acntVo_exp = null;
				expenseMainStart(true, selection, addr, port);
			} else {
				if ("No".equals(data[2].subSequence(0, 2))) {// 기간내에 정산내역 없음
					acntVo_exp = null;
					expenseMainStart(true, selection, addr, port); // 조회값이 없음
				} else {
					expVo = MEDataHandler.expInfo(data[2]);
					expenseListShow("start", sDay, eDay, pCode, aCode);
				}
			}

			stopSelf();

		} else if (function == 43) { // 미정산리스트 출력

			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			// Log.d("비용정산으로 ID보내기", myId);

			cmd = new String[3];
			data = new String[3];

			cmd[0] = "inqr_card_bill_list||" + myId + "|N|M";
			cmd[1] = "pjt_list_by_user|" + myId;
			cmd[2] = "acnt_list";

			data = SocketHandler.communicate(cmd, addr, port);

			cbVo = MEDataHandler.mjsInfo(data[0]);
			pjtVo = MEDataHandler.pjtInfo(data[1]);
			acntVo_exp = MEDataHandler.acntBasicInfo(data[2]);

			if (cbVo == null) {
				// mainRestart();
				expenseMgmtRestart();
			} else {
				Intent sendI = new Intent(SocketClientService.this, MEExpenseListMjs.class);
				// sendI.putExtra("expRegi", true);
				// sendI.putExtra("SMS", false);
				sendI.putExtra("start", true);
				sendI.putExtra("myId", myId);
				sendI.putExtra("billCode", cbVo.getExp_billCode());
				sendI.putExtra("billDate", cbVo.getExp_billDate());
				sendI.putExtra("billAmount", cbVo.getExp_billAmount());
				sendI.putExtra("shopName", cbVo.getExp_shopName());
				sendI.putExtra("shopLocation", cbVo.getExp_shopLocation());
				sendI.putExtra("state", cbVo.getExp_state());
				sendI.putExtra("processDate", cbVo.getExp_processDate());
				sendI.putExtra("type", cbVo.getExp_type());
				sendI.putExtra("pjtCode", pjtVo.getPjt_code());
				sendI.putExtra("pjtName", pjtVo.getPjt_name());
				sendI.putExtra("acntCode", acntVo_exp.getAcnt_code());
				sendI.putExtra("acntName", acntVo_exp.getAcnt_name());
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				pending(sendI);
			}
			cbVo = null;
			stopSelf();

		} else if (function == 433) { // 미정산리스트 출력

			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");

			// Log.d("SMS미정산 내역리스트 ID/sms수신여부 보내기", function + "/" + myId + "/" + insSmsMjsList);

			cmd = new String[3];
			data = new String[3];

			cmd[0] = "inqr_card_bill_list||" + myId + "|N|S";
			cmd[1] = "pjt_list_by_user|" + myId;
			cmd[2] = "acnt_list";

			data = SocketHandler.communicate(cmd, addr, port);

			cbVo = MEDataHandler.mjsInfo(data[0]);
			pjtVo = MEDataHandler.pjtInfo(data[1]);
			acntVo_exp = MEDataHandler.acntBasicInfo(data[2]);

			if (cbVo == null) {
				// mainRestart();
				expenseMgmtRestart();
			} else {
				Intent sendI = new Intent(SocketClientService.this, MEExpenseListMjsSms.class);
				// sendI.putExtra("expRegi", true);
				// sendI.putExtra("SMS", false);
				sendI.putExtra("start", true);
				sendI.putExtra("myId", myId);
				if (insSmsMjsList.equals("true")) {
					sendI.putExtra("SMS", "true");
				} else {
					sendI.putExtra("SMS", "false");
				}
				sendI.putExtra("billCode", cbVo.getExp_billCode());
				sendI.putExtra("billDate", cbVo.getExp_billDate());
				sendI.putExtra("billAmount", cbVo.getExp_billAmount());
				sendI.putExtra("shopName", cbVo.getExp_shopName());
				sendI.putExtra("shopLocation", cbVo.getExp_shopLocation());
				sendI.putExtra("state", cbVo.getExp_state());
				sendI.putExtra("processDate", cbVo.getExp_processDate());
				sendI.putExtra("type", cbVo.getExp_type());
				sendI.putExtra("pjtCode", pjtVo.getPjt_code());
				sendI.putExtra("pjtName", pjtVo.getPjt_name());
				sendI.putExtra("acntCode", acntVo_exp.getAcnt_code());
				sendI.putExtra("acntName", acntVo_exp.getAcnt_name());
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				pending(sendI);
			}
			cbVo = null;
			stopSelf();

		} else if (function == 44) { // 비용정산 : 예산 상세정보
			myId = intent.getStringExtra("myId");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];
			cmd[0] = "pjt_list_by_user|" + myId;
			data = SocketHandler.communicate(cmd, addr, port);
			pjtVo = MEDataHandler.pjtInfo(data[0]);

			if (pjtVo == null) {
				mainRestart();
			} else {
				expenseDetailStart(false);
			}
			pjtVo = null;
			stopSelf();
		} else if (function == 45) { // 정산내역등록
			myId = intent.getStringExtra("myId");
			boolean sms = intent.getBooleanExtra("sms", false);
			// String day = intent.getStringExtra("day");
			String code = intent.getStringExtra("code");
			String pjtCode = intent.getStringExtra("projectCode");
			String acntCode = intent.getStringExtra("accountCode");
			String acntName = intent.getStringExtra("accountName");
			String money = intent.getStringExtra("money");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[2];
			data = new String[2];
			cmd[0] = "pjt_list_by_user|" + myId;
			cmd[1] = "acnt_list_by_code|" + pjtCode;
			data = SocketHandler.communicate(cmd, addr, port);
			pjtVo = MEDataHandler.pjtInfo(data[0]);
			acntVo_exp = MEDataHandler.acntInfo(data[1]);

			boolean isValidAcnt = false;
			String[] compareAcnt = new String[acntVo_exp.getAcnt_name().length];
			compareAcnt = acntVo_exp.getAcnt_name();
			for (int i = 0; i < compareAcnt.length; i++) {
				if (acntName.equals(compareAcnt[i])) {
					isValidAcnt = true;
					cmd = null;
					data = null;
					break;
				}
			}
			if (!isValidAcnt) {
				acntVo_exp = null;
				cmd = null;
				data = null;
				cmd = new String[1];
				data = new String[1];
				cmd[0] = "acnt_list|";
				data = SocketHandler.communicate(cmd, addr, port);
				acntVo_exp = MEDataHandler.acntBasicInfo(data[0]);
				expenseRegisterStart(true);// 계정오류
			} else {
				cmd = new String[2];
				data = new String[2];

				Calendar c = Calendar.getInstance();
				int mYear = c.get(Calendar.YEAR);
				int mMonth = c.get(Calendar.MONTH) + 1;
				int mDay = c.get(Calendar.DAY_OF_MONTH);

				String mmYear = Integer.toString(mYear);
				String mmMonth = MEDateHandler.pad(mMonth);
				String mmDay = MEDateHandler.pad(mDay);

				cmd[0] = "insert_balance_list|" + mmYear + mmMonth + mmDay + "|" + pjtCode + "|" + acntCode + "|" + money + "|" + code;
				cmd[1] = "update_card_bill|" + code + "|Y|" + mmYear + mmMonth + mmDay;
				data = SocketHandler.communicate(cmd, addr, port);
				if ("SUCC".equals(data[0].substring(0, 4)) && "SUCC".equals(data[1].substring(0, 4))) {
					if (sms) {
						mainStart(true);
					} else {
						expenseMgmtRestart(true);
					}
				} else {
					if (!"SUCC".equals(data[0].substring(0, 4))) {
						Log.d("SocketClientService", "정산내역등록안됨(insert_balance_list 실패)");
					} else if (!"SUCC".equals(data[1].substring(0, 4))) {
						Log.d("SocketClientService", "정산내역등록안됨(update_card_bill 실패)");
					} else {

					}

					// expenseRegisterStart(true);//계정오류
				}
			}

			pjtVo = null;
			acntVo_exp = null;
			stopSelf();
		} else if (function == 46) {// 정산내역 삭제
			// Log.d("소켓 : 정산내역삭제 ", "yes");
			myId = intent.getStringExtra("myId");
			String date = intent.getStringExtra("date");
			String pCode = intent.getStringExtra("pCode");
			String aCode = intent.getStringExtra("code");
			String sDay = intent.getStringExtra("sDay");
			String eDay = intent.getStringExtra("eDay");
			String acd = intent.getStringExtra("aCode");// 삭제해도 됨..?
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[1];
			data = new String[1];
			cmd[0] = "inqr_balance_list|" + myId + "|" + date + "|" + date + "|" + pCode + "|" + aCode;
			data = SocketHandler.communicate(cmd, addr, port);
			expVo = MEDataHandler.expInfo(data[0]);
			String delNo = expVo.getExp_serial()[0];
			cmd[0] = "delete_balance_list|" + delNo;
			data = SocketHandler.communicate(cmd, addr, port);

			if ("SUCC".equals(data[0].substring(0, 4))) {
				expenseListShow("deleteSuccess", sDay, eDay, pCode, "%");
				stopSelf();
			} else {
				Log.d("SocketClientService", "정산내역삭제안됨");
				stopSelf();
			}

		} else if (function == 47) {
			myId = intent.getStringExtra("myId");
			String date = intent.getStringExtra("date");
			String money = intent.getStringExtra("money");
			String pCode = intent.getStringExtra("pCode");
			// String aCode = intent.getStringExtra("code");
			String sDay = intent.getStringExtra("sDay");
			String eDay = intent.getStringExtra("eDay");
			String acd = intent.getStringExtra("aCode");// 수정대상
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[2];
			data = new String[2];
			cmd[0] = "pjt_list_by_user|" + myId;
			cmd[1] = "acnt_list|";
			data = SocketHandler.communicate(cmd, addr, port);
			pjtVo = MEDataHandler.pjtInfo(data[0]);
			acntVo_exp = MEDataHandler.acntBasicInfo(data[1]);
			if (pjtVo == null) {
				mainRestart();
				stopSelf();
			} else {
				expenseUpdate(date, money, sDay, eDay, pCode, acd);
				stopSelf();
			}
			// pjtVo = null;
			// acntVo_exp = null;
		} else if (function == 48) {
			myId = intent.getStringExtra("myId");
			String sDay = intent.getStringExtra("sDate");
			String eDay = intent.getStringExtra("eDate");
			String spCode = intent.getStringExtra("searchPCode");
			String saCode = intent.getStringExtra("searchACode");
			String pcd = intent.getStringExtra("pCode");
			String acd = intent.getStringExtra("aCode");
			String date = intent.getStringExtra("date");
			String money = intent.getStringExtra("money");
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");

			cmd = new String[1];
			data = new String[1];
			cmd[0] = "inqr_balance_list|" + myId + "|" + date + "|" + date + "|" + spCode + "|" + saCode;
			// Log.d("계정코드불러오는 명령어", "inqr_balance_list|" + myId + "|" + date + "|" + date + "|" + spCode + "|" + saCode);
			data = SocketHandler.communicate(cmd, addr, port);
			expVo = MEDataHandler.expInfo(data[0]);
			String upNo = expVo.getExp_serial()[0];

			// Log.d("upNo", upNo);
			cmd[0] = "update_balance_list|" + pcd + "|" + acd + "|" + upNo + "|" + money;
			// Log.d("update_balance_list", cmd[0]);
			data = SocketHandler.communicate(cmd, addr, port);

			if ("SUCC".equals(data[0].substring(0, 4))) {
				expenseListShow("updateSuccess", sDay, eDay, spCode, "%");
				stopSelf();
			} else {
				Log.d("SocketClientService", "정산내역갱신안됨");
				stopSelf();
			}

		} else if (function == 49) {
			myId = intent.getStringExtra("myId");
			String pjtCode = intent.getStringExtra("projectCode");
			String nodata = "yes";
			addr = intent.getStringExtra("loginAddr");
			port = intent.getStringExtra("loginPort");
			cmd = new String[2];
			data = new String[2];
			cmd[0] = "pjt_list_by_user|" + myId;
			cmd[1] = "acnt_list_by_code|" + pjtCode;
			data = SocketHandler.communicate(cmd, addr, port);
			pjtVo = MEDataHandler.pjtInfo(data[0]);
			acntVo_exp = MEDataHandler.acntInfo(data[1]);
			if ("No".equals(data[1].substring(0, 2))) {
				nodata = "no";
			}
			Intent sendI = new Intent(SocketClientService.this, MEExpenseDetail.class);
			sendI.putExtra("detailListing", true);
			sendI.putExtra("dataYN", nodata);
			sendI.putExtra("myId", myId);
			sendI.putExtra("projectCode", pjtVo.getPjt_code());
			sendI.putExtra("projectName", pjtVo.getPjt_name());
			sendI.putExtra("acntName", acntVo_exp.getAcnt_name());
			sendI.putExtra("acntTotal", acntVo_exp.getAcnt_total());
			sendI.putExtra("acntBalance", acntVo_exp.getAcnt_balance());
			sendI.putExtra("loginAddr", addr);
			sendI.putExtra("loginPort", port);
			pending(sendI);
		}
	}

	private void expenseListShow(String Command, String start, String end, String project, String account) {
		String msg = Command;
		Intent sendI = new Intent(SocketClientService.this, MEExpenseListResult.class);
		sendI.putExtra("start", true);
		if ("start".equals(msg)) {
			sendI.putExtra("param", "no");
		} else if ("deleteSuccess".equals(msg)) {
			sendI.putExtra("param", "delOk");
		} else if ("updateSuccess".equals(msg)) {
			sendI.putExtra("param", "upOk");
		}
		sendI.putExtra("myId", myId);
		sendI.putExtra("expTime", expVo.getExp_time());// 정산일자
		sendI.putExtra("expNumber", expVo.getExp_number());// 프로젝트코드
		sendI.putExtra("expPname", expVo.getExp_pjtname());// 프로젝트 명칭
		sendI.putExtra("expAcode", expVo.getExp_acntcode());// 계정코드
		sendI.putExtra("expAname", expVo.getExp_acntname());// 계정명칭
		sendI.putExtra("expMoney", expVo.getExp_money());// 결제금액
		sendI.putExtra("expSerial", expVo.getExp_serial());
		sendI.putExtra("sDay", start);
		sendI.putExtra("eDay", end);
		sendI.putExtra("pjtCode", project);
		sendI.putExtra("acntCode", account);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void expenseMgmtRestart(boolean val) {
		Intent sendI = new Intent(SocketClientService.this, MEExpenseMgmt.class);
		sendI.putExtra("restart", true);
		sendI.putExtra("myId", myId);
		sendI.putExtra("regiOk", val);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private int listProjectRelatedAcount(String addr, String port) {
		int num = pjtVo.getPjt_code().length;
		String[] sub_cmd = new String[num];
		String[] sub_data = new String[num];
		acntVo = new MEAcntVo[num];

		for (int i = 0; i < sub_cmd.length; i++) {
			sub_cmd[i] = "acnt_list_by_code|" + (pjtVo.getPjt_code()[i]);
		}
		sub_data = SocketHandler.communicate(sub_cmd, addr, port);

		for (int i = 0; i < sub_data.length; i++) {
			acntVo[i] = MEDataHandler.acntInfo(sub_data[i]);
		}
		return num;
	}

	private void expenseRegisterStart(boolean redo) {// MEExpenseRegister 실행 : 비용정산 등록
		boolean setAcount = redo;
		Intent sendI = new Intent(SocketClientService.this, MEExpenseRegister.class);
		sendI.putExtra("start", true);
		sendI.putExtra("myId", myId);
		if (setAcount) {
			sendI.putExtra("setAcnt", true);
		}
		sendI.putExtra("projectCode", pjtVo.getPjt_code());
		sendI.putExtra("projectName", pjtVo.getPjt_name());
		sendI.putExtra("accountCode", acntVo_exp.getAcnt_code());
		sendI.putExtra("accountName", acntVo_exp.getAcnt_name());
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void expenseDetailStart(boolean redo) {// MEExpenseDetail 실행 : 예산 상세정보
		boolean setAcount = redo;
		Intent sendI = new Intent(SocketClientService.this, MEExpenseDetail.class);
		sendI.putExtra("start", true);
		if (setAcount) {
			sendI.putExtra("setAcnt", true);
		}
		sendI.putExtra("myId", myId);
		sendI.putExtra("projectCode", pjtVo.getPjt_code());
		sendI.putExtra("projectName", pjtVo.getPjt_name());
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void expenseUpdate(String val1, String val2, String start, String end, String pc, String ac) {
		String date = val1;
		String money = val2;
		String sDay = start;
		String eDay = end;
		String pCode = pc;
		String aCode = ac;
		Intent sendI = new Intent(SocketClientService.this, MEExpenseListUpdate.class);
		sendI.putExtra("updating", true);
		sendI.putExtra("myId", myId);
		sendI.putExtra("projectCode", pjtVo.getPjt_code());
		sendI.putExtra("projectName", pjtVo.getPjt_name());
		sendI.putExtra("acntCode", acntVo_exp.getAcnt_code());
		sendI.putExtra("acntName", acntVo_exp.getAcnt_name());
		sendI.putExtra("date", date);
		sendI.putExtra("money", money);
		sendI.putExtra("sDay", sDay);
		sendI.putExtra("eDay", eDay);
		sendI.putExtra("pCode", pCode);
		sendI.putExtra("aCode", aCode);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void expenseMainStart(boolean redo, String val, String addr, String port) {// MEExpenseMgmt 실행 : 정산내역조회
		boolean setAcount = redo;
		cmd = new String[1];
		data = new String[1];
		cmd[0] = "acnt_list";
		data = SocketHandler.communicate(cmd, addr, port);
		acntVo_exp = MEDataHandler.acntBasicInfo(data[0]);
		Intent sendI = new Intent(SocketClientService.this, MEExpenseListSearch.class);
		sendI.putExtra("start", true);
		if (setAcount) {
			sendI.putExtra("setAcnt", true);
			sendI.putExtra("selection", val);
		}
		sendI.putExtra("myId", myId);
		sendI.putExtra("projectCode", pjtVo.getPjt_code());
		sendI.putExtra("projectName", pjtVo.getPjt_name());
		sendI.putExtra("accountCode", acntVo_exp.getAcnt_code());
		sendI.putExtra("accountName", acntVo_exp.getAcnt_name());
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void projectMainStart(int count) {// MEPjtMain 실행 : 프로젝트관리-리스트
		Intent sendI = new Intent(SocketClientService.this, MEPjtMain.class);
		sendI.putExtra("start", true);
		sendI.putExtra("count", count);
		sendI.putExtra("projectCode", pjtVo.getPjt_code());
		sendI.putExtra("projectName", pjtVo.getPjt_name());
		sendI.putExtra("projectStart", pjtVo.getPjt_start());
		sendI.putExtra("projectEnd", pjtVo.getPjt_end());
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);

		for (int i = 0; i < count; i++) {
			sendI.putExtra("acntVo:" + i + ":acntName", acntVo[i].getAcnt_name());
			sendI.putExtra("acntVo:" + i + ":acntTotal", acntVo[i].getAcnt_total());
			sendI.putExtra("acntVo:" + i + ":acntBalance", acntVo[i].getAcnt_balance());
		}
		pending(sendI);
	}

	private void mainRestart() {// MEMain 재실행 : 프로젝트관리
		Intent sendI = new Intent(SocketClientService.this, MEMain.class);
		sendI.putExtra("restart", true);
		sendI.putExtra("myId", myId);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void expenseMgmtRestart() {
		Intent sendI = new Intent(SocketClientService.this, MEExpenseMgmt.class);
		sendI.putExtra("restart", true);
		sendI.putExtra("myId", myId);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);

	}

	private void userMainRestart() {
		Intent sendI = new Intent(SocketClientService.this, MEUsrMain.class);
		sendI.putExtra("restart", true);
		sendI.putExtra("myId", myId);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	private void mainStart(boolean val) {// MEMain 실행 : 로그인
		boolean smsYN = val;
		Intent sendI = new Intent(SocketClientService.this, MEMain.class);
		sendI.putExtra("mainstart", true);
		sendI.putExtra("myId", usrVo.getMy_id());
		sendI.putExtra("sms", smsYN);
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	// 로그인 실패(ID/PW 오류)
	private void loginRestart(String val) {
		String selection = val;
		Intent sendI = new Intent(SocketClientService.this, MEIntro.class);
		sendI.putExtra("restart", true);
		if ("wrongId".equals(selection)) {
			sendI.putExtra("selection", selection);
		} else if ("wrongPw".equals(selection)) {
			sendI.putExtra("selection", selection);
		}
		sendI.putExtra("loginAddr", addr);
		sendI.putExtra("loginPort", port);
		pending(sendI);
	}

	// 펜딩처리 해당 Activity 실행
	private void pending(Intent sendI) {
		PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 0, sendI, PendingIntent.FLAG_ONE_SHOT);

		try {
			pi.send();
		} catch (Exception e) {
			// nothing to do
			Log.d("SocketClientService", "펜딩처리 실패");
		}
	}

	public IBinder onBind(Intent intent) {
		return null;
	}

}
